﻿

using HBase;
using hcollection;
using System;
using System.Collections.Generic;

namespace hCommand {
    public abstract class AllocateJobTask :DeviceCode {

        public abstract List<BaseTask> analyseTaskModel(AbsStatus gcs);
        public abstract Type getSynchro();
        public override bool excute() {
            var type = getSynchro();
            AbsStatus gcs = StatusCache.getStatus(type);
            if( gcs == null ) {
                return true;
            }
            perCheck();
            if( !gcs.isIdle() ) {
                consoleOnly("不是空闲状态");
                return true;
            }
            if( !chechModeDB() ) {
                return true;
            }
            List<BaseTask> robotTask = analyseTaskModel(gcs);
            if( robotTask != null && robotTask.Count > 0 ) {
                DoOutAndShiftTask(robotTask);
            } else {
                robotTask = preExecution();
                if( robotTask != null && robotTask.Count > 0 ) {
                    DoOutAndShiftTask(robotTask);
                } else {
                    consoleOnly("isIdle");
                }
            }
            return true;
        }

        public virtual bool chechModeDB() {
            return true;
        }
        public virtual void perCheck() {

        }
        public virtual List<BaseTask> preExecution() {
            return new List<BaseTask>();
        }

        public List<T> getTask<T>() where T : BaseTask {
            List<T> res = new List<T>();

            List<BaseTask> list = MyCache.getList<BaseTask>(typeof(T));

            foreach( BaseTask bt in list ) {
                if( bt.status == 9 ) {
                    continue;
                }
                /*
                if( !bt.canWork() ) {
                    continue;
                }*/

                if( DeviceId() == bt.driveId ) {
                    res.Add(( T ) bt);
                }
            }

            this.consoleOnly("共{0}个任务，可执行{1}个{2}" ,list.Count ,res.Count ,typeof(T).Name);
            return res;
        }
        public bool working(int status) {
            if( status > ( int ) CoreTaskParamStatus.待执行 && status < ( int ) CoreTaskParamStatus.执行结束 ) {
                return true;
            } else {
                return false;
            }
        }
    }
}
